package rhc.taskSchedule.taskDistribute.impl;

import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;

import rhc.discribute.node.host.Host;
import rhc.taskSchedule.BasicComponent;
import rhc.taskSchedule.command.RunningCommandCreater;
import rhc.taskSchedule.commander.TaskScheduleCommanderExecute;
import rhc.taskSchedule.entity.Running;
import rhc.taskSchedule.entity.RunningType;
import rhc.taskSchedule.exception.RunningException;
import rhc.taskSchedule.taskDistribute.StartRunning;

/**基础，外部调用该类，由该类选择合适的类型来调用
 * @author rhc
 *
 */
public class BasicStartRunning extends BasicComponent implements StartRunning{

	Map<String,StartRunning> runningType_startRunning=new HashMap<String,StartRunning>();
	
	public BasicStartRunning(){
		runningType_startRunning.put(RunningType.SCHEDULE.type, new TaskScheduleStartRunning());
		runningType_startRunning.put(RunningType.TASK.type, new TaskExecuteStartRunning());
	}
	
	@Override
	public void startRunning(Running running, TaskScheduleCommanderExecute taskScheduleCommanderExecute) throws Exception {
		String runningId=running.getId();
		String runningType=running.getRunningType();
		if(StringUtils.isEmpty(runningType)){
			throw new RunningException("运行 {} 获取运行类型为空",runningId);
		}
		StartRunning startRunning=runningType_startRunning.get(runningType);
		if(null == startRunning){
			throw new RunningException("运行 {} 运行类型异常，{}",runningId,runningType);
		}
		
		startRunning.startRunning(running, taskScheduleCommanderExecute);
	}
	
	/**发送运行请求
	 * @param runningId
	 * @param taskScheduleCommander
	 * @throws Exception 
	 */
	protected void sendRunnning(String runningId,Host runHost,TaskScheduleCommanderExecute taskScheduleCommanderExecute) throws Exception{
		
		RunningCommandCreater runningCommand=taskScheduleCommanderExecute.getRunningCommandCreater();
		
		String runCommmand=runningCommand.getRunCommand(runningId);
		
		runningCommand.sendCommand(runCommmand, runHost);
	}

}
